package com.kzj.service.impl;

import com.kzj.common.constant.GlobalConstant;
import com.kzj.common.enumerate.RoleEnum;
import com.kzj.common.result.Result;
import com.kzj.common.util.ValidateCodeUtil;
import com.kzj.pojo.dto.LoginDto;
import com.kzj.service.CommonService;
import com.kzj.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    TeacherService teacherService;
    @Override
    public Result login(@RequestBody LoginDto dto, HttpSession session)
    {
        switch (RoleEnum.getRoleByValue(dto.getRole()))
        {
            case TEACHER: return teacherService.login(dto,session);
            case ADMINISTRATOR: break;
            case STUDENT: break;
        }
        return Result.error("未知角色");
    }
    @Override
    public Result authCode(HttpSession session, HttpServletResponse response)
    {
        String code=null;
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            code = ValidateCodeUtil.generateVerifyImage(outputStream);
            session.setAttribute(GlobalConstant.VERIFY_CODE,code);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }
}
